{% extends 'form.html' %}
{% block other_buttons %}
<input type="button" value="发送验证码" id="send_code" class="btn btn-info">
{% endblock %}
{% block script_extend %}
<script>
    $("#send_code").click(function () {
        $("#tip").text("")
        var email = $("#id_email").val()
        if (email == "") {
            $("#tip").text("* 邮箱不能为空")
            return false
        }
        $.ajax({
            url: "{% url 'send_verification_code' %}",
            type: "GET",
            data: {
                'email': email,
                'send_for': 'forget_password_code',
            },
            cache: false,
            success: function (data) {
                if (data['status'] != 'success') {
                    alert(data['status'])
                }
            },
        })
        // 把按钮变灰
        var old_text = $(this).val()
        $(this).addClass('disabled')
        $(this).attr('disabled', true)
        // 倒计时发送验证码
        var time = 30
        var interval = setInterval(() => {
            if (time <= 0) {
                clearInterval(interval)
                $(this).removeClass('disabled')
                $(this).attr('disabled', false)
                $(this).val(old_text)
                return false
            }
            var new_text = `${old_text}(${time})s`
            $(this).val(new_text)
            time--
        }, 1000);

    })
</script>
{% endblock %}
